package cake.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cake.dao.impl.CakeOrderDaoImpl;
import cake.dao.impl.DetailsDaoImpl;
import cake.dao.impl.UserDaoImpl;
import cake.entity.CakeOrder;
import cake.entity.Details;
import cake.entity.User;

@WebServlet("/admin/orderdetail.html")
public class AdminOrderDetailServlet extends HttpServlet {
	private DetailsDaoImpl detailsDaoImpl = new DetailsDaoImpl();
	private CakeOrderDaoImpl cakeOrderDaoImpl = new CakeOrderDaoImpl();
	private UserDaoImpl userDaoImpl = new UserDaoImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int orderid = Integer.parseInt(request.getParameter("orderid"));
		List<Details> details = detailsDaoImpl.getDetailsByOid(orderid);
		request.setAttribute("details", details);

		CakeOrder cakeOrder = cakeOrderDaoImpl.getCakeOrderByid(orderid);
		User user = userDaoImpl.getUserByUid(cakeOrder.getUid());
		String username = user.getUserName();
		request.setAttribute("cakeOrder", cakeOrder);
		request.setAttribute("username", username);
		request.getRequestDispatcher("orderdetail.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String status = request.getParameter("status");
		System.out.println(request.getParameter("status"));
		System.out.println(request.getParameter("id"));
		int id = Integer.parseInt(request.getParameter("id"));
		if(status!="") {
			cakeOrderDaoImpl.updateOrderStatus(status,id);
		}
		response.sendRedirect("orderlist.html");
	}
}
